package com.zywl.api;

import com.zywl.core.beans.Member;
import com.zywl.service.MemberService;
import com.zywl.utils.models.JsonResult;
import com.zywl.web.controller.base.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/member/")
public class MemberApi extends BaseController {


    @Autowired
    private MemberService memberService;

    @ResponseBody
    @RequestMapping("detail")
    public JsonResult detail(Long mebId){
        JsonResult jr = new JsonResult();
        try {
            if(mebId == null){
                return jr.error("用户id不能为空");
            }

            Member member = memberService.getById(mebId);
            jr.success("操作成功",member);
        }catch (Exception e){
            logger.error("获取详情失败",e);
            jr.error("获取详情失败");
        }
        return jr;
    }

    @ResponseBody
    @RequestMapping("modify")
    public JsonResult modify(Member form){
        JsonResult jr = new JsonResult();
        try {
            if(form.getMebId() == null){
                return jr.error("用户id不能为空");
            }
            Member member = memberService.getById(form.getMebId());
            if(StringUtils.isNotEmpty(form.getMebNickname())){
                member.setMebNickname(form.getMebNickname());
            }
            if(StringUtils.isNotEmpty(form.getMebMobile())){
                member.setMebMobile(form.getMebMobile());
            }
            if(StringUtils.isNotEmpty(form.getMebImg())){
                member.setMebImg(form.getMebImg());
            }
            memberService.update(member);
            jr.success("修改成功",member);
        }catch (Exception e){
            logger.error("修改失败",e);
            jr.error("修改失败");
        }
        return jr;
    }

}




